package com.wlyuan.open.scanner.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.application.annotation.DataScope;
import com.wlyuan.open.scanner.dao.entity.QuotationRelationDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2023-07-10
 */
public interface QuotationRelationMapper extends BaseMapper<QuotationRelationDO> {

    String WHERE = "where t.waybill_type = #{waybillType} and is_combine = 0 and is_quotation = 1 ";

    /**
     * 获取运单列表
     *
     * @return
     */
    @Select("select t.id,t.waybill_id as waybillId,t.waybill_type as waybillType,t.create_at as createAt,t.tenant_id as tenantId,t.is_combine as isCombine,t.is_quotation as isQuotation,quotation_at as quotationAt,is_template as isTemplate from t_quotation_relation t " +
            WHERE +
            "order by t.id asc " +
            "OFFSET ( #{pageNum} -1)* #{pageSize} ROWS FETCH next #{pageSize} rows only")
    @DataScope(column = "tenant_id", alias = "t")
    List<QuotationRelationDO> list(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("waybillType") Integer waybillType);

    @Select("select count(*) from t_quotation_relation t " + WHERE)
    @DataScope(column = "tenant_id", alias = "t")
    Integer count(@Param("waybillType") Integer waybillType);

    @Select("select count(*) from t_quotation_relation t " + WHERE + " AND t.tenant_id = #{tenantId} ")
    @DataScope(column = "tenant_id", alias = "t")
    Integer countByTenantId(@Param("tenantId") Long tenantId);

    @Select("select t.id,t.waybill_id as waybillId,t.waybill_type as waybillType,t.create_at as createAt,t.tenant_id as tenantId,t.is_combine as isCombine,t.is_quotation as isQuotation,quotation_at as quotationAt,is_template as isTemplate from t_quotation_relation t " +
            WHERE + " and t.tenant_id = #{tenantId}" +
            " order by t.id asc " +
            "OFFSET ( #{pageNum} -1)* #{pageSize} ROWS FETCH next #{pageSize} rows only")
    @DataScope(column = "tenant_id", alias = "t")
    List<QuotationRelationDO> listByTenantId(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("tenantId") Long tenantId, @Param("waybillType") Integer waybillType);

    @Select("select t.waybill_id from t_quotation_relation t " + WHERE + " AND t.create_at >= #{startTime} and t.create_at <= #{endTime}")
    @DataScope(column = "tenant_id", alias = "t")
    List<Long> getByTime(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("waybillType") Integer waybillType);

    @Select("select t.waybill_id from t_quotation_relation t " + WHERE + " AND t.tenant_id = #{tenantId} and t.create_at >= #{startTime} and t.create_at <= #{endTime}")
    @DataScope(column = "tenant_id", alias = "t")
    List<Long> getByTenantIdTime(@Param("tenantId") Long tenantId, @Param("startTime") String startTime, @Param("endTime") String endTime, @Param("waybillType") Integer waybillType);

    @Select("select top 1 t.id,t.waybill_id as waybillId,t.waybill_type as waybillType,t.create_at as createAt,t.tenant_id as tenantId,t.is_combine as isCombine,t.is_quotation as isQuotation,quotation_at as quotationAt,is_template as isTemplate from t_quotation_relation t where t.waybill_id = #{waybillId}")
    @DataScope(column = "tenant_id", alias = "t")
    QuotationRelationDO getWaybillId(@Param("waybillId") Long waybillId);
}
